package com.rayc.mall.core.interceptor;

import com.rayc.mall.common.exception.BizIllegalException;
import com.rayc.mall.common.context.AdminContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-23
 * @Description: 管理员鉴权拦截器
 * @Version: 1.0
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            // 没匹配到路径，给全局异常处理controller处理
            return true;
        }
        // 判断是否管理员
        if (!AdminContext.isAdmin()) {
            throw new BizIllegalException("非管理员操作");
        }
        return true;
    }
}
